package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.dao.BrandDao;
import com.atguigu.gulimail.product.dao.CategoryDao;
import com.atguigu.gulimail.product.entity.BrandEntity;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimail.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimail.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
	@Autowired
	private CategoryDao categoryDao;
	@Autowired
	private BrandDao brandDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public List<CategoryBrandRelationEntity> findByBrandId(Long brandId) {
		QueryWrapper<CategoryBrandRelationEntity> query = new QueryWrapper<>();
		query.eq("brand_id", brandId);
		return this.list(query);
	}

	@Override
	public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
		CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
		BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
		categoryBrandRelation.setBrandName(brandEntity.getName());
		categoryBrandRelation.setCatelogName(categoryEntity.getName());
		this.save(categoryBrandRelation);
	}

	@Override
	public void updateBrand(Long brandId, String name) {
		CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
		categoryBrandRelationEntity.setBrandId(brandId);
		categoryBrandRelationEntity.setBrandName(name);
		this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
	}

	@Override
	public void updateCategory(Long categoryId, String categoryName) {
		this.baseMapper.updateCategory(categoryId,categoryName);
	}
}